package com.bishe.app.modules.menu.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("tb_menu")
public class MenuTree implements Serializable {


    /**
     * 主键ID
     */
    @TableId(value="id",type = IdType.AUTO)
    private Long id;

    /**
     * 角色ID
     */
    private Long parentId;

    /**
     * 用户ID
     */
    private String name;

    /**
     * 用户ID
     */
    private String code;

    /**
     * 1页面 2链接
     */
    private Integer type;

    /**
     * 0未删除 1删除
     */
    private Integer isDel;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 创建时间
     */
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    private LocalDateTime updatedAt;

    /**
     * 操作用户
     */
    private String opUser;


    private List<MenuTree> children;


    public static List<Long> getLeafIds(List<Menu> menuList){
        List<MenuTree> tree = getTree(menuList);
        if(CollectionUtils.isEmpty(tree)){
            return new ArrayList<>();
        }
        List<Long> leafIds = new ArrayList<>();

        for (MenuTree menuTree : tree) {
            isLeaf(menuTree,leafIds);
        }

        return leafIds;
    }

    public static void isLeaf(MenuTree tree,List<Long> ans){
        if(tree==null){
            return;
        }
        if(CollectionUtils.isEmpty(tree.getChildren())){
           ans.add(tree.getId());
           return;
        }

        for (MenuTree child : tree.getChildren()) {
            isLeaf(child,ans);
        }
    }

    public static List<MenuTree> getTree(List<Menu> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return new ArrayList<>();
        }
        List<MenuTree> menuTree = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getParentId() == 0) {
                MenuTree root = convertMenuTree(menu);
                menuTree.add(root);
                buildTree(menuList, root);
            }
        }

        return menuTree;
    }

    private static void buildTree(List<Menu> menuList, MenuTree root) {
        for (Menu menu : menuList) {
            if (menu.getParentId().equals(root.getId())) {
                MenuTree child = convertMenuTree(menu);
                root.getChildren().add(child);
                buildTree(menuList, child);
            }
        }
    }


    private static MenuTree convertMenuTree(Menu menu) {
        MenuTree tree = new MenuTree();
        tree.setId(menu.getId());
        tree.setParentId(menu.getParentId());
        tree.setName(menu.getName());
        tree.setCode(menu.getCode());
        tree.setType(menu.getType());
        tree.setIsDel(menu.getIsDel());
        tree.setSort(menu.getSort());
        tree.setCreatedAt(menu.getCreatedAt());
        tree.setUpdatedAt(menu.getUpdatedAt());
        tree.setOpUser(menu.getOpUser());
        tree.setChildren(new ArrayList<>());
        return tree;
    }

}
